<?php
/**
 * The base controller which is used by the Front and the Admin controllers
 */

class Base_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
}

class Admin_Controller extends Base_Controller
{
    protected $uid;

    function __construct()
    {
        parent::__construct();
        //载入模板
        $this->load->set_front_theme('manage');
        // 判断登录
        if (!$this->auth->is_login()) {
            redirect(site_url('home/admin_login'));
        }
        // 链接数据库
        $this->uid = $this->session->userdata('user_id');
        if ($this->uid) {
            //取一个用户信息
            $data['user']                    = $this->user_m->get_user_by_user_id($this->uid);
            $data['user']['uname']           = get_user_name($data['user']['user_name'], $data['user']['user_uname']);
            $data['user']['user_group_name'] = $this->user_m->get_user_by_user_group($data['user']['user_group']);
            //获取头像
            $this->load->model('upload_m');
            $data['user']['big_avatar']    = $this->upload_m->get_avatar_url($this->uid, 'big');
            $data['user']['middle_avatar'] = $this->upload_m->get_avatar_url($this->uid, 'middle');
            $data['user']['small_avatar']  = $this->upload_m->get_avatar_url($this->uid, 'small');
        } else {
            $loveme = get_cookie('loveme', true);
            $salt   = get_salt();
            if (isset($loveme)) {
                if ($this->user_m->checklogin($loveme, $salt)) {
                    redirect(site_url());
                }
            }
        }
        $uid  = $this->uid;
        $con  = $this->router->fetch_class(); // 当前控制器
        $func = $this->router->fetch_method(); // 当前方法名
        // 获取当前菜单
        $current         = $this->authRule_m->get_current($con, $func);
        $data['current'] = $current[0];
        $userinfo        = $this->user_m->get_user_with_group($uid);
        $menu_access_ids = $userinfo['rules'];
        // 检查访问权限
        $allow_controller_name = ['Upload'];//放行控制器名称
        $allow_action_name     = [];//放行函数名称
        if ($userinfo['group_id'] != 1 && !$this->auth->check($con . '/' . $func,
                $uid) && !in_array($con, $allow_controller_name) && !in_array($func,
                $allow_action_name)
        ) {
            show_message('没有访问权限', site_url('home/logout'), 1);
        }
        // 获取登录用户的权限菜单
        if ($userinfo['group_id'] != 1) {
            $menu_where = "AND id in ($menu_access_ids)";
        } else {
            $menu_where = '';
        }
        $menu         = $this->authRule_m->get_menu_by_auth($menu_where);
        $menu         = $this->getMenu($menu);
        $data['menu'] = $menu;

        $data['viewmulu'] = base_url('public');
        $data['x_title']  = $this->config->item('site_short_name');
        $this->load->vars($data);
    }

    // 获取菜单
    protected function getMenu($items, $id = 'id', $pid = 'pid', $son = 'children')
    {
        $tree   = [];
        $tmpMap = [];
        //修复父类设置islink=0，但是子类仍然显示的bug
        foreach ($items as $item) {
            if ($item['pid'] == 0) {
                $father_ids[] = $item['id'];
            }
        }
        //----
        foreach ($items as $item) {
            $tmpMap[$item[$id]] = $item;
        }

        foreach ($items as $item) {
            //修复父类设置islink=0，但是子类仍然显示的bug by shaoneng
            if ($item['pid'] <> 0 && !in_array($item['pid'], $father_ids)) {
                continue;
            }
            //----
            if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
            } else {
                $tree[] = &$tmpMap[$item[$id]];
            }
        }
        return $tree;
    }
}

class Www_Controller extends Base_Controller
{
    protected $cookie_id;

    function __construct()
    {
        parent::__construct();

        //载入模板
        $this->load->set_front_theme('www');
        $data['viewmulu'] = base_url('public/www');
        $func             = $this->router->fetch_method(); // 当前方法名
        $data['func']     = $func;
        // 全局友链
        $this->load->model('links_m');
        $links         = $this->links_m->select(['hide' => 1]);
        $data['links'] = $links;

        $this->load->model('articleCategory_m');
        $category         = $this->articleCategory_m->get_all();
        $data["category"] = $category;

        $this->load->model('zixunCategory_m');
        $zixuncategory         = $this->zixunCategory_m->get_all();
        $data["zixuncategory"] = $zixuncategory;

        $this->load->model('onlineCategory_m');
        $onlinecategory         = $this->onlineCategory_m->get_all();
        $data["onlinecategory"] = $onlinecategory;

        // 品牌故事
        $this->load->model('page_m');
        $brand         = $this->page_m->select("type=0");
        $data["brand"] = $brand;
        // 艾克服务
        $service         = $this->page_m->select("type=1");
        $data["service"] = $service;
        // 加盟艾克
        $join         = $this->page_m->select("type=2");
        $data["join"] = $join;

        $func = $this->router->fetch_method(); // 当前方法名
        $data["func"] = $func ?? "index";
        // pp($data);exit();
        $this->load->vars($data);
    }
}
